/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mbeans;

import java.util.StringTokenizer;
import util.jWMI;

/**
 *
 * @author alan
 */
public class AgentWindows implements AgentWindowsMBean {

    private static Long memoriaTotal;
    private static Long memoriaLivre;
    private static Long memoriaOcupada;
    //processador
    private String processador;
    private String frequencia;
    private String numeroNucleos;
    private String numeroNucleosLogico;
    //
    private String arquiteturaOS;
    private String stoppedServicos;
    private String runningServicos;
    private String processos;
    private String serialNumber;
    private String descUnidadeDisco;
    private String volumeUnidDisco;
    private String sistemaOperacional;
    private String oSVersion;
    //impressora
    private String impressora;
    private String aplicacoes;
    private static String nomeBean = "AgentWindows";

    public Long getMemoriaLivre() {
        try {
            String memoria = jWMI.getWMIValue("select * from Win32_OperatingSystem", "FreePhysicalMemory");
            Long AuxMemoria;
            memoriaLivre = new Long("0");
            for (StringTokenizer stringTokenizer = new StringTokenizer(memoria); stringTokenizer.hasMoreTokens();) {
                String token = stringTokenizer.nextToken();
                AuxMemoria = Long.parseLong(token);
                memoriaLivre += AuxMemoria;
            }
            return memoriaLivre / 1024;
        } catch (Exception ex) {
            return Long.getLong("0");
        }
    }

    public String getoSVersion() {
        try {
            oSVersion = jWMI.getWMIValue("select * from Win32_OperatingSystem", "CSDVersion");
            return oSVersion;
        } catch (Exception ex) {
            return "";
        }
    }

    public String getNumeroNucleos() {
        try {
            numeroNucleos = jWMI.getWMIValue("select * from Win32_Processor", "NumberOfCores");
            return numeroNucleos;
        } catch (Exception ex) {
            return "";
        }
    }

    public String getNumeroNucleosLogico() {
        try {
            numeroNucleosLogico = jWMI.getWMIValue("select * from Win32_Processor", "NumberOfLogicalProcessors");
            return numeroNucleosLogico;
        } catch (Exception ex) {
            return "";
        }
    }

    public String getVolumeUnidDisco() {
        try {
            volumeUnidDisco = jWMI.getWMIValue("select * from Win32_Volume WHERE name like '%:%' AND Capacity is not null", "caption, capacity/1024/1024/1024,FreeSpace/1024/1024/1024, FileSystem");
            return volumeUnidDisco;
        } catch (Exception ex) {
            return "";
        }
    }

    public String getAplicacoes() {
        try {
            aplicacoes = jWMI.getWMIValue("select * from Win32_Product", "Name");
            return aplicacoes;
        } catch (Exception ex) {
            return "";
        }
    }

    @Override
    public Long getMemoriaOcupada() {
        try {
            memoriaOcupada = (getMemoriaTotal() - getMemoriaLivre());
            return memoriaOcupada;
        } catch (Exception e) {
            return Long.getLong("0");
        }

    }

    public String getSistemaOperacional() {
        sistemaOperacional = System.getProperty("os.name");
        return sistemaOperacional;
    }

    public Long getMemoriaTotal() {
        try {
            String memoria;
            Long AuxMemoria;
            memoriaTotal = new Long("0");
            memoria = jWMI.getWMIValue("select * from Win32_PhysicalMemory", "Capacity");
            for (StringTokenizer stringTokenizer = new StringTokenizer(memoria); stringTokenizer.hasMoreTokens();) {
                String token = stringTokenizer.nextToken();
                AuxMemoria = Long.parseLong(token);
                memoriaTotal += AuxMemoria;
            }
            return memoriaTotal / 1024 / 1024;
        } catch (Exception ex) {
            return Long.getLong("0");
        }
    }

    public synchronized String pegaNomeMbean() {
        return this.nomeBean;
    }

    public String getImpressora() {
        try {
            impressora = jWMI.getWMIValue("select Name,PaperSize from Win32_PrinterConfiguration", "Name");
            return impressora;
        } catch (Exception ex) {
            return "";
        }
    }

    public String getProcessador() {
        try {
            processador = jWMI.getWMIValue("select * from Win32_Processor", "name");
            return processador;
        } catch (Exception ex) {
            return "";
        }
    }

    public String getSerialNumber() {
        try {
            serialNumber = jWMI.getWMIValue("select * from Win32_OperatingSystem", "SerialNumber");
            return serialNumber;
        } catch (Exception ex) {
            return "";
        }
    }

    public String getArquiteturaOS() {
        try {
            arquiteturaOS = jWMI.getWMIValue("select * from Win32_OperatingSystem", "OSArchitecture");
            return arquiteturaOS;
        } catch (Exception ex) {
            return "";
        }
    }

    public String getProcessos() {
        try {
            processos = jWMI.getWMIValue("select * from Win32_Process", "Caption");
            return processos;
        } catch (Exception ex) {
            return "";
        }
    }

    public String getRunningServicos() {
        try {
            runningServicos = jWMI.getWMIValue("select * from Win32_Service WHERE state='Running'", "DisplayName");
            return runningServicos;
        } catch (Exception ex) {
            return "";
        }
    }

    public String getStoppedServicos() {
        try {
            stoppedServicos = jWMI.getWMIValue("select * from Win32_Service WHERE state='Stopped'", "DisplayName");
            return stoppedServicos;
        } catch (Exception ex) {
            return "";
        }
    }

    public String getFrequencia() {
        try {
            frequencia = jWMI.getWMIValue("select * from Win32_Processor", "CurrentClockSpeed");
            return frequencia;
        } catch (Exception ex) {
            return "";
        }
    }
}
